package com.youlai.boot.system.service.order;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import com.youlai.boot.system.model.entity.LpsWl;
import com.youlai.boot.system.model.vo.CommonOrderVO;
import com.youlai.boot.system.model.vo.LpsOrderPjVO;
import com.youlai.boot.util.MapUtils;
import com.youlai.boot.util.NumberUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
@Slf4j
public class PjOrderHandle {

    public final static String KZU = "曳引比 KZU";
    public final static String BKS = "Distance between guides BKS";
    public final static String ELEVATOR_SYSTEM = "ELEVATOR_SYSTEM";
    public final static String CAR_SLING_VARIANT = "CAR_SLING_VARIANT";
    public final static String CAR_SAFETY_GEAR_TYPE = "CAR_SAFETY_GEAR_TYPE";
    public final static String AN_ZHUANG = "安装所在国家或地区";
    public final static String WL_57670954 = "57670954";
    public final static String WL_59503269 = "59503269";

    /**
     * @param objList
     * @param lpsWlMap
     * @return
     */
    public List<LpsOrderPjVO> handle(List<CommonOrderVO> objList, Map<String, LpsWl> lpsWlMap) {
        if (CollectionUtil.isEmpty(objList)) {
            return CollectionUtil.newArrayList();
        }
        List<LpsOrderPjVO> list = new ArrayList<>();
        objList.stream().forEach(item -> {
            LpsOrderPjVO order = new LpsOrderPjVO();
            BeanUtils.copyProperties(item, order);
            if (Objects.nonNull(lpsWlMap.get(order.getWl()))) {
                order.setBz(lpsWlMap.get(order.getWl()).getRemark());
            }
            Map<String, String> featureMap = MapUtils.getFeatureMap(item.getTz());

            switch (order.getWl()) {
                case WL_57670954:
                    WL_57670954(featureMap, order);
                    break;
                case WL_59503269:
                    WL_59503269(featureMap, order);
                    break;
            }
            order.setId(IdUtil.getSnowflakeNextId());
            list.add(order);
        });
        return list;
    }

    private void WL_59503269(Map<String, String> featureMap, LpsOrderPjVO order) {
        try {
            order.setBks(featureMap.getOrDefault(BKS, ""));
            BigDecimal cdjgzd = NumberUtils.getLengthValue(order.getBks(), -462);
            order.setCdjgzd(cdjgzd.toString());
        } catch (Exception e) {
            order.setBz("错误:BKS解析成数字错误,触动机构长度无法计算");
        }
    }

    /**
     * ELEVATOR_SYSTEM等于6、其他无参数，起重管长度为312；
     * ELEVATOR_SYSTEM等于5、KZU等于1、CAR_SLING_VARIANT等于3、CAR_SAFETY_GEAR_TYPE大于等于7，起重管长度为312；
     * ELEVATOR_SYSTEM等于5、KZU等于1、CAR_SLING_VARIANT等于4、CAR_SAFETY_GEAR_TYPE大于等于7，起重管长度为312；
     * ELEVATOR_SYSTEM等于5、KZU等于2、CAR_SLING_VARIANT等于3、CAR_SAFETY_GEAR_TYPE等于7，起重管长度为312；
     * ELEVATOR_SYSTEM等于5、KZU等于2、CAR_SLING_VARIANT等于3、CAR_SAFETY_GEAR_TYPE不等于7，起重管长度为260；
     * ELEVATOR_SYSTEM等于5、KZU等于2、CAR_SLING_VARIANT等于1、CAR_SAFETY_GEAR_TYPE无参数，起重管长度为260；
     * ELEVATOR_SYSTEM等于5、KZU等于2、CAR_SLING_VARIANT等于2、CAR_SAFETY_GEAR_TYPE无参数，起重管长度为260；
     *
     * @param featureMap
     * @param order
     */
    private void WL_57670954(Map<String, String> featureMap, LpsOrderPjVO order) {
        try {
            order.setAzszgjhdq(featureMap.getOrDefault(AN_ZHUANG, ""));
            order.setElevatorSystem(featureMap.getOrDefault(ELEVATOR_SYSTEM, ""));
            order.setKzu(featureMap.getOrDefault(KZU, ""));
            order.setCarSlingVariant(featureMap.getOrDefault(CAR_SLING_VARIANT, ""));
            order.setCarSafetyGearType(featureMap.getOrDefault(CAR_SAFETY_GEAR_TYPE, ""));

            Double esValue = NumberUtils.getDouble(order.getElevatorSystem());
            Double kzuValue = NumberUtils.getDouble(order.getKzu());
            Double csvValue = NumberUtils.getDouble(order.getCarSlingVariant());
            Double csgtValue = NumberUtils.getDouble(order.getCarSafetyGearType());

            String qzgzd = "错误";
            if (esValue == 6) {
                qzgzd = "312";
            } else if (esValue == 5) {
                if (kzuValue == 1 && csvValue != null && (csgtValue != null && csgtValue >= 7)) {
                    if (csvValue == 3 || csvValue == 4) {
                        qzgzd = "312";
                    }
                } else if (kzuValue == 2 && csvValue == 3) {
                    if (csgtValue != null && csgtValue == 7) {
                        qzgzd = "312";
                    } else {
                        qzgzd = "260";
                    }
                } else if (kzuValue == 2 && (csvValue == 1 || csvValue == 2)) {
                    qzgzd = "260";
                }
            }
            order.setQzgzd(qzgzd);
        } catch (Exception e) {
            order.setBz("错误:ELEVATOR_SYSTEM,KZU,CAR_SLING_VARIANT,CAR_SAFETY_GEAR_TYPE 解析成数字错误,起重管长度无法计算");
        }
    }
}
